package com.itheima.pay.controller;


import com.alipay.easysdk.kernel.Config;
import com.github.wxpay.sdk.WXPay;
import com.itheima.pay.config.WXPayConfigCustom;
import com.itheima.pay.config.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
public class WiXinPayController {

    @Autowired
    Config config;

    /**
     * 微信获取收款二维码
     * @param
     */
    @GetMapping("wxPay")
    public Map<String, String> wxPay(String code) {
        WXPayConfigCustom wxPayConfigCustom = new WXPayConfigCustom();
        try {
            WXPay wxPay = new WXPay(wxPayConfigCustom);
            Map<String, String> map = new HashMap<>();
            map.put("body","测试微信扫码支付");
            map.put("out_trade_no",code);
            map.put("total_fee","1");
            map.put("trade_type","NATIVE");
            map.put("device_info","");
            map.put("fee_type","CNY");
            map.put("notify_url","http://itheima.ngrok2.xiaomiqiu.cn/wxpay/notify");
            map.put("spbill_create_ip","123.12.12.123");


            Map<String, String> wxPayResult = wxPay.unifiedOrder(map);
            return wxPayResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }





    /**
     * 测试微信收款查询
     * @param
     */
    @GetMapping("orderQuery")
    public Map<String, String> orderQuery(String code) {
        WXPayConfigCustom wxPayConfigCustom = new WXPayConfigCustom();
        try {
            WXPay wxPay = new WXPay(wxPayConfigCustom);
            Map<String, String> map = new HashMap<>();
//            map.put("body","测试微信扫码支付");
            map.put("out_trade_no",code);
//            map.put("total_fee","1");
//            map.put("trade_type","NATIVE");
//            map.put("device_info","");
//            map.put("fee_type","CNY");
//            map.put("notify_url","http://itheima.ngrok2.xiaomiqiu.cn/wxpay/notify");
//            map.put("spbill_create_ip","123.12.12.123");


            Map<String, String> wxPayResult = wxPay.orderQuery(map);
            return wxPayResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }





    /**
     * 测试微信退款
     * @param
     */
    @GetMapping("refund")
    public Map<String, String> refund(String code) {
        WXPayConfigCustom wxPayConfigCustom = new WXPayConfigCustom();
        try {
            WXPay wxPay = new WXPay(wxPayConfigCustom);
            Map<String, String> map = new HashMap<>();
//            map.put("body","测试微信扫码支付");
            map.put("out_trade_no",code);
            map.put("out_refund_no",code);
            map.put("total_fee","1");
            map.put("refund_fee","1");
//            map.put("device_info","");
//            map.put("fee_type","CNY");
//            map.put("notify_url","http://itheima.ngrok2.xiaomiqiu.cn/wxpay/notify");
//            map.put("spbill_create_ip","123.12.12.123");


            Map<String, String> wxPayResult = wxPay.refund(map);
            return wxPayResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }








    /**
     * 测试微信退款查询
     * @param
     */
    @GetMapping("refundQuery")
    public Map<String, String> refundQuery(String code) {
        WXPayConfigCustom wxPayConfigCustom = new WXPayConfigCustom();
        try {
            WXPay wxPay = new WXPay(wxPayConfigCustom);
            Map<String, String> map = new HashMap<>();
//            map.put("body","测试微信扫码支付");
            map.put("out_trade_no",code);
//            map.put("out_refund_no",code);
//            map.put("total_fee","1");
//            map.put("refund_fee","1");
//            map.put("device_info","");
//            map.put("fee_type","CNY");
//            map.put("notify_url","http://itheima.ngrok2.xiaomiqiu.cn/wxpay/notify");
//            map.put("spbill_create_ip","123.12.12.123");


            Map<String, String> wxPayResult = wxPay.refundQuery(map);
            return wxPayResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
